var app = getApp()
var verify = require('../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '设备信息',
    })
    var deviceId = options.deviceId
    var type = options.type
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      deviceId:deviceId,
      type:type
    })
  },

  // 获取设备信息
  getDeviceInfo(){
    var deviceId = this.data.deviceId
    var type = this.data.type

    var token = app.globalData.token
    var appUrl = app.globalData.appbaseUrl
    var that = this 

    wx.request({
      url: appUrl + 'device/' + deviceId + '/info',
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("deviceInfo = ",res.data.data)
          var deviceInfo = res.data.data
          that.setData({
            deviceInfo:deviceInfo
          })
          if (type =="deviceStorage"){
            that.getSpecList();
          }
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
            success(){
              wx.navigateBack({
                
              })
            }
          })
        }
      }
    }) 
  },

  // 获取全部的产品规格下拉框
  getSpecList(){
    var productId = this.data.deviceInfo.productId

    var token = app.globalData.token
    var appUrl = app.globalData.appbaseUrl
    var that = this

    wx.request({
      url: appUrl + 'get_spec_list?productId=' + productId,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("getSpecList = ", res)
          var specList = res.data.data
          that.setData({
            specList: specList
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    }) 
  },
  // 选择产品规格
  chooseSpec(res){
    console.log("chooseSpec = ",res.detail.value)
    var index = res.detail.value
    this.setData({
      index:index
    })
  },

  // 测试通过/不通过
  testOK(res){
    var deviceId = this.data.deviceId
    var testResult = res.currentTarget.dataset.testresult

    var token = app.globalData.token
    var appUrl = app.globalData.appbaseUrl
    var that = this
    
    wx.request({
      url: appUrl + 'device/' + deviceId + '/test_feedback?testResult=' + testResult,
      method:"put",
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.navigateTo({
            url: 'test-ok/test-ok?testResult=' + testResult,
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    }) 
  },

  // 设备入库
  deviceStorage(res){
    var testResult = res.currentTarget.dataset.testresult
    var index = this.data.index
    var specList = this.data.specList
    var specs = specList[index]
    var deviceId = this.data.deviceId
    console.log("specs = ", specs)
    console.log("deviceId = ", deviceId)

    var token = app.globalData.token
    var appUrl = app.globalData.appbaseUrl
    var that = this

    wx.request({
      url: appUrl + 'device/' + deviceId + '/storage?specs=' + specs,
      method: "put",
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.navigateTo({
            url: 'test-ok/test-ok?testResult=' + testResult,
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDeviceInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})